﻿<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" Theme="MSN_Blue" %>

<script runat="server">
    
    Dim name As String = ""
    Dim types1 As types = New types()
    
    Public Sub clearfields_Click()
        txt_id_type.Text = ""
        txt_id_image.Text = ""
        txt_name.Text = ""
    End Sub
    
    Protected Sub clear_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        clearfields_Click()
    End Sub

    Public Sub formfieldtovariable() 'coloca os campos da variavel para o form
        
        types1.id_type = Integer.Parse(txt_id_type.Text)
        types1.id_image = Integer.Parse(txt_id_image.Text)
        types1.name = txt_name.Text
    End Sub
    
    Protected Sub btn_insert_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        formfieldtovariable()
                
        lbl_info.Text = "Success." + types1.insert()
        lbl_info.ForeColor = Drawing.Color.Green
    End Sub

    Protected Sub btn_update_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        formfieldtovariable()
        
        lbl_info.Text = "Success." + types1.update()
        lbl_info.ForeColor = Drawing.Color.Green
    End Sub

    
    Public Sub variabletoformfield() 'coloca os campos da variavle para
        
        txt_id_type.Text = types1.id_type.ToString()
        txt_id_image.Text = types1.id_image.ToString()
        txt_name.Text = types1.name
    End Sub
    Protected Sub btn_first_Click(ByVal sender As Object, ByVal e As System.EventArgs)
                
        types1.first()      
        variabletoformfield()
    
    End Sub

    Protected Sub btn_last_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        types1.last()
        variabletoformfield()
        
    End Sub
    
    
    Protected Sub btn_next_Click(ByVal sender As Object, ByVal e As System.EventArgs)
           
        formfieldtovariable()
        types1.mnext()
        variabletoformfield()
        
    End Sub

    Protected Sub btn_previous_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        formfieldtovariable()
        types1.previous()
        variabletoformfield()
    End Sub

    Protected Sub btn_delete_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        formfieldtovariable()
        
        lbl_info.Text = "Success." + types1.delet()
        lbl_info.ForeColor = Drawing.Color.Green
        
        txt_id_type.Text = 0
        txt_id_image.Text = 0
        txt_name.Text = ""
        
    End Sub
    
    Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim connection_string As String = ConfigurationManager.ConnectionStrings("conn").ConnectionString

        Dim sqlConnection1 As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection()
        sqlConnection1.ConnectionString = connection_string
        sqlConnection1.Open()

        Dim sqlcomand1 As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand()

        Dim String_sql As String = "SELECT * FROM types"
        
        sqlcomand1.Connection = sqlConnection1
        sqlcomand1.CommandText = String_sql
        GridView1.DataKeyNames = New String() {"id_type"}
        GridView1.DataSource = sqlcomand1.ExecuteReader()
        GridView1.DataBind()
        
        sqlConnection1.Close()
        
    End Sub
    
    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        'Page.ClientScript.RegisterStartupScript("".GetType(), "ClientScript", "alert ('Mensagem');", True)
        'txt_id_type.Text = GridView1.SelectedValue.ToString()
        'txt_id_image.Text = GridView1.SelectedRow.Cells(2).Text
        'txt_name.Text = GridView1.SelectedRow.Cells(3).Text
        
        types1.id_type = GridView1.SelectedValue.ToString()
        types1.select_type_by_id() ' vai à base de dados
        variabletoformfield()
    End Sub

    Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs)
        'Page.ClientScript.RegisterStartupScript("".GetType(), "ClientScript", "alert ('id:" + GridView1.Rows(e.RowIndex).Cells(2).Text + "');", True)
        
        types1.id_type = Integer.Parse(GridView1.Rows(e.RowIndex).Cells(2).Text)
        types1.delet()
        clearfields_Click()
 
        'jccc.utilidades.utilidades_java.mensagem(Page, jccc.utilidades.encriptacao.gerapassword(8))
    End Sub
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <style type="text/css">
        .style1
        {
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <h1>
        Types</h1>
    <table width="100%">
        <tr>
            <td class="style1">
                <asp:Label ID="Label1" runat="server" Text="id_type"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txt_id_type" runat="server" Text="0" Style="margin-left: 0px" Width="724px" ReadOnly=true></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style1">
                <asp:Label ID="Label2" runat="server" Text="id_image"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txt_id_image" runat="server" Width="726px" Text="0"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style1">
                <asp:Label ID="Label3" runat="server" Text="name"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txt_name" runat="server" Width="728px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style1">
                
            </td>
            <td>

                <asp:Button ID="clear" runat="server" Text="Clear" Width="49px" 
                    onclick="clear_Click" style="margin-left: 0px" />
                <asp:Button ID="btn_insert" runat="server" onclick="btn_insert_Click" Text="Insert" />
                <asp:Button ID="btn_update" runat="server" onclick="btn_update_Click" Text="Update" />
                <asp:Button ID="btn_first" runat="server" Text="<<" onclick="btn_first_Click" />
                <asp:Button ID="btn_previous" runat="server" Text="<" 
                    onclick="btn_previous_Click" />
                <asp:Button ID="btn_next" runat="server" Text=">" onclick="btn_next_Click" />
                <asp:Button ID="btn_last" runat="server" Text=">>" onclick="btn_last_Click" />
                <asp:Button ID="btn_delete" runat="server" Text="Delete" 
                    onclick="btn_delete_Click" />
                <asp:Label ID="lbl_info" runat="server"></asp:Label>
            </td>
        </tr>
        <tr>
            <td class="style1" colspan="2">
                
                <asp:GridView ID="GridView1" runat="server" 
                    onselectedindexchanged="GridView1_SelectedIndexChanged" 
                    onrowdeleting="GridView1_RowDeleting">
                    <Columns>
                        <asp:CommandField HeaderText="select " ShowHeader="True" 
                            ShowSelectButton="True" />
                        <asp:CommandField HeaderText="delete" ShowDeleteButton="True" 
                            ShowHeader="True" />
                    </Columns>
                </asp:GridView>
                
            </td>
        </tr>
    </table>
</asp:Content>
